<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateCategoryStatisticsRequest;
use App\Http\Requests\UpdateCategoryStatisticsRequest;
use App\Models\CategoryStatistics;
use App\Models\Goods;
use App\Models\GoodsAttribute;
use App\Repositories\CategoryStatisticsRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Illuminate\Support\Facades\DB;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class CategoryStatisticsController extends AppBaseController
{
    /** @var  CategoryStatisticsRepository */
    private $categoryStatisticsRepository;

    public function __construct(CategoryStatisticsRepository $categoryStatisticsRepo)
    {
        $this->categoryStatisticsRepository = $categoryStatisticsRepo;
    }

    /**
     * Display a listing of the CategoryStatistics.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->categoryStatisticsRepository->pushCriteria(new RequestCriteria($request));
        $categoryStatistics = CategoryStatistics::orderBy('date','desc')->paginate(20);
        foreach ($categoryStatistics as $categoryStatistic) {
            $categoryStatistic->category = DB::table('categories')->where('id',$categoryStatistic->category_id)->first();
        }
        return view('category_statistics.index')
            ->with('categoryStatistics', $categoryStatistics);
    }

    /**
     * Show the form for creating a new CategoryStatistics.
     *
     * @return Response
     */
    public function create()
    {
        return view('category_statistics.create');
    }

    /**
     * Store a newly created CategoryStatistics in storage.
     *
     * @param CreateCategoryStatisticsRequest $request
     *
     * @return Response
     */
    public function store(CreateCategoryStatisticsRequest $request)
    {
        $input = $request->all();

        $categoryStatistics = $this->categoryStatisticsRepository->create($input);

        Flash::success('Category Statistics saved successfully.');

        return redirect(route('categoryStatistics.index'));
    }

    /**
     * Display the specified CategoryStatistics.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $categoryStatistics = $this->categoryStatisticsRepository->findWithoutFail($id);

        if (empty($categoryStatistics)) {
            Flash::error('Category Statistics not found');

            return redirect(route('categoryStatistics.index'));
        }
        $categoryStatistics->load('category');
        $categoryStatistics->info = json_decode($categoryStatistics->json,true);

        $goods_arr = [];
        $goods = Goods::all();
        foreach ($goods as $v) {
            $goods_arr[$v->id] = $v;
        }

        $attribute_arr = [];
        $attributes = GoodsAttribute::all();
        foreach ($attributes as $v) {
            $attribute_arr[$v->id] = $v;
        }

        return view('category_statistics.show')->with('categoryStatistics', $categoryStatistics)->with('attribute_arr', $attribute_arr)->with('goods_arr', $goods_arr);
    }

    /**
     * Show the form for editing the specified CategoryStatistics.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $categoryStatistics = $this->categoryStatisticsRepository->findWithoutFail($id);

        if (empty($categoryStatistics)) {
            Flash::error('Category Statistics not found');

            return redirect(route('categoryStatistics.index'));
        }

        return view('category_statistics.edit')->with('categoryStatistics', $categoryStatistics);
    }

    /**
     * Update the specified CategoryStatistics in storage.
     *
     * @param  int              $id
     * @param UpdateCategoryStatisticsRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateCategoryStatisticsRequest $request)
    {
        $categoryStatistics = $this->categoryStatisticsRepository->findWithoutFail($id);

        if (empty($categoryStatistics)) {
            Flash::error('Category Statistics not found');

            return redirect(route('categoryStatistics.index'));
        }

        $categoryStatistics = $this->categoryStatisticsRepository->update($request->all(), $id);

        Flash::success('Category Statistics updated successfully.');

        return redirect(route('categoryStatistics.index'));
    }

    /**
     * Remove the specified CategoryStatistics from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $categoryStatistics = $this->categoryStatisticsRepository->findWithoutFail($id);

        if (empty($categoryStatistics)) {
            Flash::error('Category Statistics not found');

            return redirect(route('categoryStatistics.index'));
        }

        $this->categoryStatisticsRepository->delete($id);

        Flash::success('Category Statistics deleted successfully.');

        return redirect(route('categoryStatistics.index'));
    }

    public function refresh($id)
    {
        $categoryStatistics = $this->categoryStatisticsRepository->findWithoutFail($id);

        if (empty($categoryStatistics)) {
            Flash::error('Category Statistics not found');

            return redirect(route('categoryStatistics.index'));
        }
        $this->categoryStatisticsRepository->generate($categoryStatistics->category_id, 1, $categoryStatistics->type, $categoryStatistics->date);
        Flash::success('Category refresh successfully.');

        return redirect(route('categoryStatistics.index'));
    }
}
